import WxService from '../libs/wx-service/WxService';
import WxValidate from '../libs/wx-validate/WxValidate';
import _ from '../libs/underscore.modified';
import wxParse from '../libs/wxParse/wxParse';
import Promise from '../libs/es6-promise';
import coordtransform from'../libs/coordtransform';
import Api from '../utils/api';
import PubSub from '../libs/pubsub';
class Global{
  constructor(){
    this.WxService = new WxService;
    this._ = _;                                                      
    this.wxParse = wxParse;
    this.Promise = Promise;
    this.WxValidate = WxValidate;
    this.coordtransform = coordtransform;
    this.Api = new Api;
    this.pubsub = new PubSub();
  }
  showMsg(msg) {
    this.WxService.showToast({
      title: msg,
      icon: 'none'
    });
    return new Promise(resolve => {
      setTimeout(() => {
        resolve()
      }, 2e3);
    })
  }
  //获取当前page
  getCurrentPage() {
    var pages = getCurrentPages();
    var page = pages[pages.length - 1];
    return page;
  }
  showLoginDialog(isRefresh) {
    var page = this.getCurrentPage();
    var loginDialog = page.selectComponent("#j-login-dialog");
    loginDialog.show(isRefresh);
  }
  hideLoginDialog() {
    var page = this.getCurrentPage();
    var loginDialog = page.selectComponent("#j-login-dialog");
    loginDialog.hide();
  }
  //获得配置
  getConfig() {
    return new Promise(resolve => {
      var app = getApp();
      if (app.globalData._config) {
        resolve(app.globalData._config);
      } else {
        app._configCallback.push(function (obj) {
          resolve(obj);
        });
      }
    })
  }
  //微信登录
  wxLogin() {
    return this.WxService.login().then(res => {
      return this.Api.getOpenid({
        code: res.code
      }).then(obj => {
        const app = getApp();
        app.globalData.wxUser = obj;
        return obj;
      });
    })
  }
  //获取用户信息
  initUserInfo(data) {
    var app = getApp();
    return new Promise(resolve => {
      this.Api.getUserInfo(data).then(obj => {
        resolve(obj);
      })
    })
  }
  //获取用户信息
  getUser() {
    return new Promise(resolve => {
      var app = getApp();
      var user = app.globalData.user;
      if (user) {
        resolve(user);
      } else {
        app.loginCallback.push(function (user) {
          resolve(user);
        })
      }
    })
  }
  //获取微信用户信息
  getWxUser() {
    return new Promise(resolve => {
      var app = getApp();
      var user = app.globalData.wxUser;
      console.log(user)
      if (user) {
        resolve(user);
      } else {
        app.loginCallback.push(function (user) {
          resolve(user);
        })
      }
    })
  }
  initUidLogin(uid) {
    return this.initCommonLogin({
      'kw': uid
    });
  }
  initCommonLogin(data) {
    return new Promise((resolve, reject) => {
      this.initUserInfo(data).then(obj => {
        if (obj.status === 'error') {
          reject();
        } else {
          var data = obj.data;
          //这个肯定是登录成功了才能获取数据
          data.uid = data.id;
          //这边统一更新wxUser，可能手机号登录的用户并不是微信用户
          this.setUser(data);
          resolve(data);
        }
      })
    })
  }
  //登陆
  initLogin() {
    return new Promise(resolve => {
      this.wxLogin().then(obj => {
        var app = getApp();
        if (obj.id) {
            app.globalData.wxUser = obj;
            this._.each(app.loginCallback, function (v) {
              v(app.globalData.wxUser);
            })
        } else {
          console.log(obj)
          app.globalData.wxUser = obj;
          this._.each(app.loginCallback, function (v) {
            v(app.globalData.wxUser);
          })
        }
        resolve(obj);
      });
    })
  }
}

export default Global;
